<%-- 
    Document   : index.jsp
    Created on : 13 déc. 2014, 10:30:00
    Author     : coload12
--%>

<%@page import="java.text.NumberFormat"%>
<%@page import="java.text.DecimalFormat"%>
<%@page import="webapps.tools.Tools"%>
<%@page import="java.util.ArrayList"%>
<%@page import="java.util.Iterator"%>
<%@page import="java.util.List"%>
<%@page import="metier.pojo.Client"%>
<%@page contentType="text/html" pageEncoding="UTF-8"%>
<!DOCTYPE html>
<html>
    <head>
        <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
        <title>JSP Page</title>
        <!-- Latest compiled and minified CSS -->
        <link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.1/css/bootstrap.min.css">

        <!-- Optional theme -->
        <link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.1/css/bootstrap-theme.min.css">

        <!-- Latest compiled and minified JavaScript -->
        <script src="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.1/js/bootstrap.min.js"></script>
        
        <script src="//code.jquery.com/jquery-1.11.2.min.js"></script>
        <style>
            .montant
            {
                text-align: right;
                padding-right : 20px;
            }
        </style>
    </head>
    <body>
        <h1>Liste de tous les clients</h1>
        <ol class="breadcrumb">
            <li class="active">Liste des utilisateurs</li>
        </ol>
        <%
            Tools.displayMessage(request, out, webapps.servlet.AdminController.message);
        %>
        <%
            NumberFormat formatter= new DecimalFormat("###,###,###.00 €"); 
        %>
        <h2>Filtre</h2>
        <form class="form-inline" role="form" method="post">
            <input type="hidden" name="action" value="search" /> 
            <div class="form-group">
                <input name="id" type="number" class="form-control" id="exampleInputPassword2" placeholder="identifiant">
            </div>
            <div class="form-group">
                <div class="input-group">
                    <input name="mc" type="text" class="form-control" id="exampleInputEmail2" placeholder="Prénom ou nom">
                </div>
            </div>
            <div class="form-group">
                <input name="ville" type="text" class="form-control" id="exampleInputPassword2" placeholder="Ville">
            </div>
            <button type="submit" class="btn btn-default">Filtrer</button>
        </form>

        <div class="container">
            Nombre résultats : <span style="font-weight: bold;"><%= request.getAttribute("nombre_resultat") != null ? (Integer) request.getAttribute("nombre_resultat") : 0%></span>
        </div>

        <div class="container">
            <%
                ArrayList<metier.tools.Filter> filtres = (ArrayList<metier.tools.Filter>) request.getAttribute("filtres");
                if (filtres != null) {
                    for (metier.tools.Filter unfiltre : filtres) {
                        out.println("<span class=\"label label-default\"> " + unfiltre.getColonne() + " : " + unfiltre.getValue());
                        out.println("<span class=\"glyphicon glyphicon-remove\" aria-hidden=\"true\"></span>");
                        out.println("</span>");
                    }
                }
            %>
        </div>
        <table class="table table-hover">
            <tr>
                <th>Numero</th>
                <th>Nom</th>
                <th>Prénom</th>
                <th>Email</th>
                <th>Ville</th>
                <th>Nombre de comptes</th>
                <th class="montant">Montant</th>
                <th>Etat</th>
                <th>Commande</th>
            </tr>
            <%
                List<Client> biblio = ((List<Client>) request.getAttribute("clients"));
                Iterator<Client> it = biblio.iterator();
                while (it.hasNext()) {
                    Client unclient = it.next();
            %>
            <tr>
                <td><%= unclient.getId()%></td>
                <td><%= unclient.getNom()%></td>
                <td><%= unclient.getPrenom()%></td>
                <td><a href="mailto:<%= unclient.getEmail()%>"><%= unclient.getEmail()%></a></td>
                <td><%= unclient.getVille()%></td>
                <td><%= unclient.getComptes().size()%></a></td>
                <td class="montant"><%= formatter.format( unclient.getMontant() ) %> </td>
                <td><%= unclient.getEtat()%></td>
                <td>
                    <a href="AdminCompteController?id=<%= unclient.getId() %>">[voir ses comptes]</a>
                    <a href="">[consulter]</a>
                    <a href="">[modifier]</a>
                    <a href="?action=supprimer&id=<%= unclient.getId()%>">[supprimer]</a>
                </td>
            </tr>
            <%
                }

            %>
        </table>

        <nav style="text-align:center;">
            <ul class="pagination">
                <%                    int numero_page = 1;

                    if (request.getAttribute("numero_page") != null) {
                        numero_page = ((Integer) request.getAttribute("numero_page"));
                    }

                    if (numero_page != 1) {
                %>
                <li>
                    <a href="?p=<%= numero_page - 1%>" aria-label="Previous">
                        <span aria-hidden="true">&laquo;</span>
                    </a>
                </li>
                <%
                    }
                %>
                <%
                    //PAGINER
                    int nombre_page = 1;

                    if (request.getAttribute("nombre_page") != null) {
                        nombre_page = ((Integer) request.getAttribute("nombre_page"));
                    }

                    for (int cpt = 1; cpt < numero_page; cpt++) {
                        out.println("<li><a href='?p=" + cpt + "'>" + cpt + "</a></li>");
                    }

                    out.println("<li class='active'><a href='?p=" + numero_page + "'>" + numero_page + "</a></li>");

                    for (int cpt = numero_page + 1; cpt <= nombre_page; cpt++) {
                        out.println("<li><a href='?p=" + cpt + "'>" + cpt + "</a></li>");
                    }
                %>
                <%
                    if (nombre_page != numero_page) {
                %>
                <li>
                    <a href="?p=<%= numero_page + 1%>" aria-label="Next">
                        <span aria-hidden="true">&raquo;</span>
                    </a>
                </li>
                <%
                    }
                %>
            </ul>
        </nav>

        <button style="float:right;margin-right: 20px;" type="button" class="btn btn-default">Ajouter un client</button>
    </body>
</html>
